package com.czx.common.utils;

import java.util.Random;

public class RandomCodeUtil {
    public static void main(String[] args) {
        String code = generateRandomCode(4);
        System.out.println("随机生成的验证码：" + code);
    }

    public static String generateRandomCode(int length) {
        // 随机数生成器
        Random random = new Random();

        // 验证码字符集
        String characters = "0123456789";

        StringBuilder code = new StringBuilder();
        for (int i = 0; i < length; i++) {
            // 从字符集中随机选择一个字符
            int index = random.nextInt(characters.length());
            char ch = characters.charAt(index);
            code.append(ch);
        }

        return code.toString();
    }
}
